package com.agiantii.ablog.controller;

import com.agiantii.ablog.common.Constant;
import com.agiantii.ablog.common.R;
import com.agiantii.ablog.pojo.dto.PostDTO;
import com.agiantii.ablog.pojo.Post;
import com.agiantii.ablog.service.PostService;
import com.agiantii.ablog.util.StringUtil;
import com.agiantii.ablog.pojo.vo.PostVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("posts")
public class PostController {
    
    @Resource
    private PostService postService;
    
    @PostMapping("user/{userId}")
    public R<Post> createPost(@PathVariable Long userId ,PostDTO postDTO) {
        log.info("createPost userId:{}, postDTO:{}", userId, postDTO);
        if(StringUtil.isEmpty(postDTO.getCover())){
            postDTO.setCover(Constant.DEFAULT_COVER);
        }
        return postService.createPost(userId, postDTO);
    }
    
    @PutMapping("{postId}")
    public R<PostVO> updatePost( @PathVariable Long postId,PostDTO postDTO, Long userId) {
        log.info("updatePost postId:{}, userId:{}, postDTO:{}", postId, userId, postDTO);
        return postService.updatePost(postId, userId, postDTO);
    }
    
    @DeleteMapping("{postId}")
    public R<Void> deletePost(@PathVariable Long postId, Long userId) {
        log.info("deletePost postId:{}, userId:{}", postId, userId);
        return postService.deletePost(postId, userId);
    }
    
    @GetMapping("{postId}")
    public R<PostVO> getPost(@PathVariable Long postId) {
        return postService.getPost(postId);
    }
    
    @GetMapping("user/{userId}")
    public R<List<PostVO>> getUserPosts(@PathVariable Long userId, Integer page, Integer size) {
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        return postService.getUserPosts(userId, page, size);
    }
    
    @GetMapping
    public R<List<PostVO>> getAllPosts(Integer page, Integer size) {
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        return postService.getAllPosts(page, size);
    }
    
    @GetMapping("search")
    public R<List<PostVO>> searchPosts(String keyword, 
                                     @RequestParam(defaultValue = "TITLE") String searchType,
                                     Integer page, 
                                     Integer size) {
        log.info("searchPosts keyword:{}, searchType:{}, page:{}, size:{}", 
                keyword, searchType, page, size);
        if (keyword == null || keyword.trim().isEmpty()) {
            return R.error("搜索关键词不能为空", 400);
        }
        
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        
        String searchTypeDesc;
        switch (searchType.toUpperCase()) {
            case "TITLE":
                searchTypeDesc = "标题";
                break;
            case "CONTENT":
                searchTypeDesc = "内容";
                break;
            case "AUTHOR":
                searchTypeDesc = "作者";
                break;
            default:
                searchTypeDesc = "未知";
        }
        log.info("搜索类型: {}", searchTypeDesc);
        
        return postService.search(keyword, searchType, page, size);
    }
} 